{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np;import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     apple\n",
       "1    orange\n",
       "2     apple\n",
       "3     apple\n",
       "4     apple\n",
       "5    orange\n",
       "6     apple\n",
       "7     apple\n",
       "dtype: object"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "In [11]: values = pd.Series(['apple', 'orange', 'apple',\n",
    "....: 'apple'] * 2)\n",
    "In [12]: values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['apple', 'orange'], dtype=object)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "values.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "apple     6\n",
       "orange    2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "values.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    1\n",
       "2    0\n",
       "3    0\n",
       "4    0\n",
       "5    1\n",
       "6    0\n",
       "7    0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "In [15]: values = pd.Series([0, 1, 0, 0] * 2)\n",
    "In [16]: dim = pd.Series(['apple', 'orange'])\n",
    "In [17]: values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     apple\n",
       "1    orange\n",
       "dtype: object"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     apple\n",
       "1    orange\n",
       "0     apple\n",
       "0     apple\n",
       "0     apple\n",
       "1    orange\n",
       "0     apple\n",
       "0     apple\n",
       "dtype: object"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dim.take(values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>basket_id</th>\n",
       "      <th>fruit</th>\n",
       "      <th>count</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>apple</td>\n",
       "      <td>12</td>\n",
       "      <td>2.773544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>orange</td>\n",
       "      <td>3</td>\n",
       "      <td>1.107153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>apple</td>\n",
       "      <td>12</td>\n",
       "      <td>1.086464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>apple</td>\n",
       "      <td>8</td>\n",
       "      <td>3.009836</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>apple</td>\n",
       "      <td>3</td>\n",
       "      <td>3.473764</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>orange</td>\n",
       "      <td>6</td>\n",
       "      <td>3.751267</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>apple</td>\n",
       "      <td>10</td>\n",
       "      <td>3.657617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>apple</td>\n",
       "      <td>12</td>\n",
       "      <td>3.359603</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   basket_id   fruit  count    weight\n",
       "0          0   apple     12  2.773544\n",
       "1          1  orange      3  1.107153\n",
       "2          2   apple     12  1.086464\n",
       "3          3   apple      8  3.009836\n",
       "4          4   apple      3  3.473764\n",
       "5          5  orange      6  3.751267\n",
       "6          6   apple     10  3.657617\n",
       "7          7   apple     12  3.359603"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "In [20]: fruits = ['apple', 'orange', 'apple', 'apple'] * 2\n",
    "In [21]: N = len(fruits)\n",
    "In [22]: df = pd.DataFrame({'fruit': fruits,\n",
    "....: 'basket_id': np.arange(N),'count': np.random.randint(3, 15,size=N),\n",
    "....: 'weight': np.random.uniform(0, 4,size=N)},\n",
    "....: columns=['basket_id', 'fruit', 'count','weight'])\n",
    "In [23]: df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "fruit_cat = df.fruit.astype('category')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     apple\n",
       "1    orange\n",
       "2     apple\n",
       "3     apple\n",
       "4     apple\n",
       "5    orange\n",
       "6     apple\n",
       "7     apple\n",
       "Name: fruit, dtype: category\n",
       "Categories (2, object): [apple, orange]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruit_cat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_categories = pd.Categorical(['foo','bar','foo','baz'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[foo, bar, foo, baz]\n",
       "Categories (3, object): [bar, baz, foo]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "In [34]: categories = ['foo', 'bar', 'baz']\n",
    "In [35]: codes = [0, 1, 2, 0, 0, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_cats_2 = pd.Categorical.from_codes(codes,categories)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[foo, bar, baz, foo, foo, bar]\n",
       "Categories (3, object): [foo, bar, baz]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_cats_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "ordered_cat = pd.Categorical.from_codes(codes,categories,ordered=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[foo, bar, baz, foo, foo, bar]\n",
       "Categories (3, object): [foo < bar < baz]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ordered_cat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[foo, bar, baz, foo, foo, bar]\n",
       "Categories (3, object): [foo, bar, baz]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_cats_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[foo, bar, baz, foo, foo, bar]\n",
       "Categories (3, object): [foo < bar < baz]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_cats_2.as_ordered()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "In [41]: np.random.seed(12345)\n",
    "In [42]: draws = np.random.randn(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-2.04707659e-01,  4.78943338e-01, -5.19438715e-01, -5.55730304e-01,\n",
       "        1.96578057e+00,  1.39340583e+00,  9.29078767e-02,  2.81746153e-01,\n",
       "        7.69022568e-01,  1.24643474e+00,  1.00718936e+00, -1.29622111e+00,\n",
       "        2.74991633e-01,  2.28912879e-01,  1.35291684e+00,  8.86429341e-01,\n",
       "       -2.00163731e+00, -3.71842537e-01,  1.66902531e+00, -4.38569736e-01,\n",
       "       -5.39741446e-01,  4.76985010e-01,  3.24894392e+00, -1.02122752e+00,\n",
       "       -5.77087303e-01,  1.24121276e-01,  3.02613562e-01,  5.23772068e-01,\n",
       "        9.40277775e-04,  1.34380979e+00, -7.13543985e-01, -8.31153539e-01,\n",
       "       -2.37023165e+00, -1.86076079e+00, -8.60757398e-01,  5.60145293e-01,\n",
       "       -1.26593449e+00,  1.19827125e-01, -1.06351245e+00,  3.32882716e-01,\n",
       "       -2.35941881e+00, -1.99542955e-01, -1.54199553e+00, -9.70735912e-01,\n",
       "       -1.30703025e+00,  2.86349747e-01,  3.77984111e-01, -7.53886535e-01,\n",
       "        3.31285650e-01,  1.34974221e+00,  6.98766888e-02,  2.46674110e-01,\n",
       "       -1.18616011e-02,  1.00481159e+00,  1.32719461e+00, -9.19261558e-01,\n",
       "       -1.54910644e+00,  2.21845987e-02,  7.58363145e-01, -6.60524328e-01,\n",
       "        8.62580083e-01, -1.00319021e-02,  5.00093559e-02,  6.70215594e-01,\n",
       "        8.52965032e-01, -9.55868852e-01, -2.34933207e-02, -2.30423388e+00,\n",
       "       -6.52468841e-01, -1.21830198e+00, -1.33260971e+00,  1.07462269e+00,\n",
       "        7.23641505e-01,  6.90001853e-01,  1.00154344e+00, -5.03087391e-01,\n",
       "       -6.22274225e-01, -9.21168608e-01, -7.26213493e-01,  2.22895546e-01,\n",
       "        5.13161009e-02, -1.15771947e+00,  8.16706936e-01,  4.33609606e-01,\n",
       "        1.01073695e+00,  1.82487521e+00, -9.97518248e-01,  8.50591099e-01,\n",
       "       -1.31577601e-01,  9.12414152e-01,  1.88210680e-01,  2.16946144e+00,\n",
       "       -1.14928205e-01,  2.00369736e+00,  2.96101523e-02,  7.95253156e-01,\n",
       "        1.18109754e-01, -7.48531548e-01,  5.84969738e-01,  1.52676573e-01,\n",
       "       -1.56565729e+00, -5.62540188e-01, -3.26641392e-02, -9.29006202e-01,\n",
       "       -4.82572646e-01, -3.62638461e-02,  1.09539006e+00,  9.80928477e-01,\n",
       "       -5.89487686e-01,  1.58170009e+00, -5.28734826e-01,  4.57001871e-01,\n",
       "        9.29968759e-01, -1.56927061e+00, -1.02248698e+00, -4.02826924e-01,\n",
       "        2.20486863e-01, -1.93401108e-01,  6.69158336e-01, -1.64898482e+00,\n",
       "       -2.25279725e+00, -1.16683222e+00,  3.53607102e-01,  7.02110171e-01,\n",
       "       -2.74569205e-01, -1.39142188e-01,  1.07657222e-01, -6.06545125e-01,\n",
       "       -4.17064408e-01, -1.70070368e-02, -1.22414528e+00, -1.80083991e+00,\n",
       "        1.63473620e+00,  9.89008302e-01,  4.57940143e-01,  5.55154410e-01,\n",
       "        1.30671972e+00, -4.40553570e-01, -3.01350280e-01,  4.98791490e-01,\n",
       "       -8.23991040e-01,  1.32056584e+00,  5.07964786e-01, -6.53437675e-01,\n",
       "        1.86979514e-01, -3.91725249e-01, -2.72292975e-01, -1.71414356e-02,\n",
       "        6.80320749e-01,  6.35512357e-01, -7.57176502e-01,  7.18085834e-01,\n",
       "       -3.04273076e-01, -1.67779025e+00,  4.26986085e-01, -1.56373985e+00,\n",
       "       -3.67487521e-01,  1.04591253e+00,  1.21995436e+00, -2.47699116e-01,\n",
       "       -4.16232132e-01, -1.16747004e-01, -1.84478762e+00,  2.06870785e+00,\n",
       "       -7.76967474e-01,  1.44016687e+00, -1.10557360e-01,  1.22738699e+00,\n",
       "        1.92078426e+00,  7.46433038e-01,  2.22465959e+00, -6.79400410e-01,\n",
       "        7.27368782e-01, -8.68730734e-01, -1.21385091e+00, -4.70630931e-01,\n",
       "       -9.19241697e-01, -8.38826689e-01,  4.35155305e-01, -5.57804717e-01,\n",
       "       -5.67454871e-01, -3.72641553e-01, -9.26556901e-01,  1.75510839e+00,\n",
       "        1.20980999e+00,  1.27002473e+00, -9.74378127e-01, -6.34709255e-01,\n",
       "       -3.95700752e-01, -2.89435900e-01, -7.34297072e-01, -7.28504679e-01,\n",
       "        8.38775073e-01,  2.66893213e-01,  7.21194339e-01,  9.10982642e-01,\n",
       "       -1.02090261e+00, -1.41341604e+00,  1.29660784e+00,  2.52275209e-01,\n",
       "        1.12748110e+00, -5.68363447e-01,  3.09362168e-01, -5.77385473e-01,\n",
       "       -1.16863407e+00, -8.25019972e-01, -2.64440949e+00, -1.52985803e-01,\n",
       "       -7.51921003e-01, -1.32609252e-01,  1.45729970e+00,  6.09511845e-01,\n",
       "       -4.93779257e-01,  1.23997988e+00, -1.35722140e-01,  1.43004181e+00,\n",
       "       -8.46852451e-01,  6.03282130e-01,  1.26357226e+00, -2.55490556e-01,\n",
       "       -4.45688380e-01,  4.68366681e-01, -9.61603924e-01, -1.82450454e+00,\n",
       "        6.25428156e-01,  1.02287238e+00,  1.10742460e+00,  9.09370895e-02,\n",
       "       -3.50108657e-01,  2.17957016e-01, -8.94813130e-01, -1.74149395e+00,\n",
       "       -1.05225574e+00,  1.43660279e+00, -5.76207386e-01, -2.42029443e+00,\n",
       "       -1.06232963e+00,  2.37372262e-01,  9.57369064e-04,  6.52531808e-02,\n",
       "       -1.36752411e+00, -3.02800519e-02,  9.40489321e-01, -6.42436751e-01,\n",
       "        1.04017925e+00, -1.08292226e+00,  4.29213588e-01, -2.36223669e-01,\n",
       "        6.41817816e-01, -3.31660557e-01,  1.39407223e+00, -1.07674194e+00,\n",
       "       -1.92465982e-01, -8.71187651e-01,  4.20851997e-01, -1.21141107e+00,\n",
       "       -2.58866912e-01, -5.81646850e-01, -1.26042063e+00,  4.64574793e-01,\n",
       "       -1.07024091e+00,  8.04222698e-01, -1.56735508e-01,  2.01039001e+00,\n",
       "       -8.87104430e-01, -9.77936232e-01, -2.67217350e-01,  4.83337822e-01,\n",
       "       -4.00332733e-01,  4.49880415e-01,  3.99593953e-01, -1.51574804e-01,\n",
       "       -2.55793406e+00,  1.60806841e-01,  7.65250677e-02, -2.97204166e-01,\n",
       "       -1.29427402e+00, -8.85180013e-01, -1.87496526e-01, -4.93560000e-01,\n",
       "       -1.15412964e-01, -3.50744607e-01,  4.46973764e-02, -8.97756316e-01,\n",
       "        8.90873502e-01, -1.15118516e+00, -2.61230270e+00,  1.14125019e+00,\n",
       "       -8.67135525e-01,  3.83583258e-01, -4.37030164e-01,  3.47488810e-01,\n",
       "       -1.23017904e+00,  5.71078139e-01,  6.00612128e-02, -2.25523994e-01,\n",
       "        1.34972614e+00,  1.35029973e+00, -3.86653322e-01,  8.65989542e-01,\n",
       "        1.74723360e+00, -1.41024614e+00, -3.78241525e-01, -3.45820667e-01,\n",
       "        3.80062465e-01,  1.88994673e-01,  1.32329820e+00, -2.26458859e+00,\n",
       "       -9.14978611e-01, -4.78964164e-01,  1.04718450e+00,  9.23948418e-01,\n",
       "       -1.14150141e-01,  4.05802443e-01,  2.88451808e-01, -4.34788492e-01,\n",
       "        3.58755633e-01, -3.88244944e-01,  2.12874629e+00,  1.40960468e+00,\n",
       "       -1.05434278e-01,  7.00428367e-01,  2.09285188e+00, -1.36971796e-01,\n",
       "       -9.30489423e-01,  3.27497234e-01,  1.30301308e+00, -1.40940236e+00,\n",
       "       -1.44125945e-01, -7.16414024e-01,  1.03614186e-01, -1.49571856e+00,\n",
       "       -1.17489356e+00,  2.61399909e+00, -6.89307399e-01, -7.51652636e-01,\n",
       "        6.36280961e-01, -1.15764416e+00,  6.14679924e-01,  1.02139111e+00,\n",
       "        6.68272492e-01, -8.09535482e-01, -9.08124573e-01,  1.51228939e+00,\n",
       "        9.51174320e-02,  1.18466855e+00,  6.37032934e-01, -5.39274587e-01,\n",
       "       -5.51009929e-02, -1.13592579e+00, -1.70506254e-01, -1.15808727e+00,\n",
       "        1.10459914e+00,  6.34238100e-01,  1.25968335e+00,  9.64930776e-01,\n",
       "       -4.34445938e-01, -8.79602860e-01, -6.94838230e-01,  1.22637405e+00,\n",
       "        4.57278649e-01,  1.15698625e-01,  1.01404235e+00, -1.13500772e+00,\n",
       "       -2.63370582e-01,  1.30642518e+00, -1.61084101e+00, -1.02662069e+00,\n",
       "        1.24157279e+00, -1.56759532e-01, -2.44909553e+00, -1.03394802e+00,\n",
       "        1.59953363e+00,  4.74070770e-01,  1.51325927e-01, -5.42173166e-01,\n",
       "       -4.75496217e-01,  1.06402612e-01, -1.30822819e+00,  2.17318475e+00,\n",
       "        5.64561217e-01, -1.90480765e-01, -9.16934390e-01, -9.75813606e-01,\n",
       "        2.21230269e+00,  7.39306826e-02,  1.81859456e+00, -1.58153100e+00,\n",
       "       -7.74363409e-01,  5.52936493e-01,  1.06061390e-01,  3.92752804e+00,\n",
       "       -2.55125599e-01,  8.54137315e-01, -3.64806532e-01,  1.31101737e-01,\n",
       "       -6.97613896e-01,  1.33564946e+00, -1.51038858e-01,  4.42937851e-01,\n",
       "        9.41571221e-01,  5.33363953e-01,  3.56266193e-01, -1.01153190e-02,\n",
       "        1.41575317e+00,  5.66105533e-01,  4.56487352e-01,  1.94788105e-01,\n",
       "       -6.55053757e-01, -5.65230094e-01,  3.17687312e+00,  9.59532542e-01,\n",
       "       -9.75339835e-01, -1.11674210e+00, -1.10437634e+00, -8.98755175e-01,\n",
       "       -1.36663236e+00,  4.51401732e-01, -1.58722181e+00, -7.32788830e-01,\n",
       "       -5.14562627e-01, -6.11876053e-01, -3.15986670e-02,  2.34127888e-01,\n",
       "        2.71936281e-01,  1.23076935e+00,  1.28830616e+00,  8.51813508e-01,\n",
       "       -1.52918061e+00, -1.55171499e+00,  2.97292917e-01,  3.44791201e-01,\n",
       "       -3.98103360e-01,  4.29404057e-01, -2.85944649e-01, -2.22843700e+00,\n",
       "        6.65587850e-02,  4.89964761e-01,  1.86792585e+00,  2.07043780e+00,\n",
       "       -2.45383227e-01,  7.62302108e-01,  1.29015475e-01,  6.27075914e-01,\n",
       "       -1.06223474e+00, -1.49950345e+00,  5.45154193e-01,  4.00823267e-01,\n",
       "       -1.94623039e+00,  5.05031784e-01, -9.10489138e-01, -2.19696285e-01,\n",
       "        4.08055383e-01, -6.03145411e-01, -3.61132594e-01,  5.64024524e-01,\n",
       "       -1.05661656e+00,  1.39195005e+00, -1.76126767e+00, -9.11633128e-01,\n",
       "        6.58339264e-01, -1.57946584e+00,  4.57705976e-01, -1.83268092e-01,\n",
       "       -6.85483896e-01,  1.06431315e-01, -3.18236084e-01,  4.33712973e-01,\n",
       "        5.71825929e-01,  5.67106029e-01,  8.15769654e-02, -3.02334871e-01,\n",
       "       -7.26916388e-01,  1.80335113e-01, -5.20208734e-01,  3.98092081e-01,\n",
       "       -9.16935074e-01, -8.26501345e-02, -1.93969081e+00,  1.40799436e+00,\n",
       "        1.51240649e+00,  5.26493120e-01, -2.66930833e-01,  8.62284048e-01,\n",
       "        8.38030660e-02, -1.87233890e+00, -9.62791017e-01,  8.00668292e-02,\n",
       "        1.28726442e-01, -4.79120340e-01, -6.40280504e-01,  7.45973801e-01,\n",
       "       -6.22547063e-01,  9.36289315e-01,  7.50018377e-01, -5.67150282e-02,\n",
       "        2.30067451e+00,  5.69497467e-01,  1.48940968e+00,  1.26425028e+00,\n",
       "       -7.61837213e-01, -3.31616898e-01, -1.75131543e+00,  6.28894111e-01,\n",
       "        2.82501864e-01, -1.33813943e+00, -5.00606850e-01,  1.21645030e-01,\n",
       "        1.70832347e+00, -9.70999448e-01, -6.19332343e-01, -7.26708132e-01,\n",
       "        1.22165542e+00,  5.03699288e-01, -1.38787408e+00,  2.04851420e-01,\n",
       "        6.03705216e-01,  5.45680309e-01,  2.35477019e-01,  1.11834994e-01,\n",
       "       -1.25150375e+00, -2.94934350e+00,  6.34634161e-01,  1.24157016e-01,\n",
       "        1.29762249e+00, -1.68693341e+00,  1.08953905e+00,  2.06088174e+00,\n",
       "       -2.41235326e-01, -9.47872180e-01,  6.76294029e-01, -6.53356162e-01,\n",
       "       -6.52295298e-01,  5.28827604e-01,  3.57793249e-01,  1.88649360e-01,\n",
       "        8.69416879e-01, -5.06674481e-02, -7.16364575e-01, -1.03258721e-01,\n",
       "       -1.14103658e+00, -5.00776901e-01, -3.89301370e-01, -4.73850530e-01,\n",
       "        1.28664304e-01,  1.53694305e-01,  4.44790058e-01,  1.28531667e-01,\n",
       "        2.52529866e-01, -9.40638663e-01,  1.00214545e+00, -5.25414984e-01,\n",
       "       -8.87400936e-01,  1.83131360e+00, -9.23029332e-01,  7.00537687e-01,\n",
       "       -8.92151198e-01,  2.30074000e+00, -8.17765299e-01,  5.13759632e-01,\n",
       "        6.23586943e-01,  1.48920593e+00,  1.94047867e+00,  5.43237129e-01,\n",
       "        5.06190912e-01,  1.66201449e+00, -1.18920250e+00,  9.35974490e-02,\n",
       "       -5.39163905e-01, -1.43739560e+00,  1.87937386e-01, -4.50454457e-01,\n",
       "       -5.16878232e-01, -9.56356677e-02,  3.16423805e-01,  6.03334657e-01,\n",
       "       -1.49459146e+00, -1.10894079e-01,  2.41289404e-01, -5.82645109e-01,\n",
       "       -2.41112652e-01,  2.36360537e-01,  1.24720725e-01,  1.04632598e+00,\n",
       "       -2.73091856e-01, -5.34834020e-01, -3.06563305e-01, -1.62242665e-01,\n",
       "       -1.08323220e+00,  7.08401493e-01,  1.52074304e+00,  2.90343183e-01,\n",
       "       -6.83066330e-01, -9.50312866e-01,  4.00709936e-01, -1.26071684e-01,\n",
       "        3.98204888e-01,  1.41638473e-01, -2.64141422e-01, -4.52212074e-01,\n",
       "        7.58201973e-01, -5.15583498e-01, -5.91202322e-01,  8.96745784e-01,\n",
       "       -9.71437524e-01,  1.84080991e+00,  1.53881232e-01, -2.74083943e-01,\n",
       "       -1.78492569e+00,  9.81006686e-01, -8.73717140e-01, -1.01563442e+00,\n",
       "       -4.11243537e-01,  1.46562117e+00, -1.00621906e+00, -9.02147762e-01,\n",
       "        7.52769143e-01, -4.90508527e-01, -5.24672210e-01, -6.99195861e-01,\n",
       "        3.52360939e-01,  6.81025983e-02, -9.30341707e-01,  8.45399560e-01,\n",
       "        1.64723816e-02,  8.44962955e-01,  1.85083395e+00,  2.20742409e-02,\n",
       "       -1.36917902e+00,  8.87203523e-01,  1.43311821e-02, -7.41547051e-02,\n",
       "       -4.85647878e-02,  1.23502145e+00, -4.33294924e-01,  1.39103546e+00,\n",
       "        8.20210741e-01, -2.47423465e-01,  3.02270746e-01,  5.43980361e-01,\n",
       "       -9.42368504e-01, -1.26638281e+00,  9.37249545e-01, -7.20102245e-01,\n",
       "       -1.59395154e+00, -3.75497816e-01, -9.58703834e-01,  7.94336400e-01,\n",
       "       -1.60510784e+00,  5.43710253e-01,  9.25166364e-01, -1.46962860e+00,\n",
       "       -3.99592346e-01,  1.41734264e+00, -8.97608668e-01,  1.84480502e+00,\n",
       "        1.25316821e+00, -1.49093242e+00, -2.77339246e-02,  1.37523596e+00,\n",
       "       -2.52081701e-02, -6.67880179e-01, -2.86801753e+00,  2.10688543e-01,\n",
       "        1.28715531e+00, -5.74305988e-01,  4.95326647e-01,  3.96049590e-01,\n",
       "        5.88798190e-01, -1.28175713e+00,  2.02992261e+00, -5.01944516e-01,\n",
       "       -1.59284566e-01, -1.49621630e+00,  1.14477139e-02,  4.19445985e-01,\n",
       "        2.05121388e+00, -3.68765333e-01, -1.68925468e+00,  1.47681161e-01,\n",
       "       -1.80998392e-01,  1.58059054e-01, -3.96615422e-01, -4.00236630e-01,\n",
       "       -8.24895666e-01, -2.44440446e-01,  1.21945743e+00, -4.33630492e-01,\n",
       "        8.61183873e-01, -3.34503693e-01,  1.59559959e-01, -9.84164476e-01,\n",
       "        7.54084974e-01, -2.84391662e-01,  3.24797530e-01, -8.85424602e-01,\n",
       "       -1.28089348e+00,  1.96109935e-01,  9.54644156e-01, -8.00971332e-01,\n",
       "        1.58514730e-02,  1.08755329e+00, -6.31242820e-01, -2.26893249e-02,\n",
       "        6.85879242e-01,  5.19179208e-01,  1.82701892e-01,  2.04647381e-01,\n",
       "       -2.65986356e-01, -2.27288704e-04,  1.23945232e+00, -8.19715256e-01,\n",
       "       -2.60388907e-01,  5.19140257e-01,  1.43091645e-01, -1.16677747e-01,\n",
       "        1.49674411e+00, -1.48427438e+00, -1.67118276e+00,  9.17173409e-01,\n",
       "       -7.58014151e-01,  2.06479240e+00, -8.50778396e-01,  4.99450713e-01,\n",
       "       -7.92663655e-02, -1.40329264e+00,  1.57894791e+00,  3.69028988e-04,\n",
       "        9.00884914e-01, -4.54869220e-01, -8.64546645e-01,  1.12911990e+00,\n",
       "        5.78744129e-02, -4.33738666e-01,  9.26976374e-02, -1.39782015e+00,\n",
       "        1.45782265e+00, -1.76756916e-01, -2.54240300e-01, -1.26343750e+00,\n",
       "        4.52262741e-01, -8.40117409e-01, -5.02678071e-01,  5.13392587e-01,\n",
       "        1.64165300e+00,  5.80790036e-01, -1.70734027e+00, -1.78355431e-01,\n",
       "       -8.28459954e-01,  1.28631168e+00, -4.06452362e-01,  1.56632047e-01,\n",
       "        5.21066804e-02,  9.55813177e-01,  7.43191501e-01, -4.86323084e-01,\n",
       "        1.92046727e+00, -6.52749023e-01, -1.73303777e-01, -3.60410082e-01,\n",
       "       -3.80413977e-01, -1.29813981e+00,  5.27919008e-01, -9.31002763e-02,\n",
       "        4.01184681e-01, -1.02583380e-01,  3.08690977e-02,  2.61610051e+00,\n",
       "       -7.85577945e-01, -5.06998121e-01, -2.01820572e+00, -6.76853138e-01,\n",
       "        2.66674368e+00,  1.45145615e+00,  6.34628855e-01, -5.02826864e-01,\n",
       "        5.12931659e-01,  1.75677937e+00, -9.74310801e-01,  6.80397048e-01,\n",
       "        9.55798726e-01,  1.50153548e+00, -7.56265648e-01,  4.73504604e-01,\n",
       "        1.71374345e+00, -1.14769922e+00,  2.90322050e-03, -1.10057036e+00,\n",
       "       -2.97531782e-01,  5.02409078e-01, -9.87418981e-04, -6.74560278e-01,\n",
       "        2.97958279e-01,  1.46557314e+00, -3.03628594e-01, -9.94479885e-01,\n",
       "        1.89889991e-01, -1.68402957e+00, -4.58380742e-01,  5.43405908e-01,\n",
       "       -1.18726426e+00, -4.12641693e-01,  1.17712535e+00, -3.13704165e-01,\n",
       "        1.57903162e+00,  3.75388236e-01, -1.56813882e+00, -9.00886519e-01,\n",
       "        6.52345519e-01,  8.71600314e-01,  2.68216170e-01,  9.47681220e-01,\n",
       "        1.47267588e-01, -1.77245546e+00,  5.92419611e-01,  9.03254745e-02,\n",
       "        6.51121454e-01, -8.11946962e-02,  8.01897603e-01,  1.39845227e-01,\n",
       "       -5.01002762e-01, -1.28302559e-01,  4.14605966e-01,  6.04577786e-01,\n",
       "        2.13409475e+00,  9.41187837e-01, -9.31456796e-01, -1.24667539e-01,\n",
       "        2.00696291e-01,  1.80256286e-01, -3.20370097e-01, -1.59612803e+00,\n",
       "       -1.28169898e+00,  1.50258575e+00,  6.53538002e-01, -3.19536626e-01,\n",
       "        9.55094011e-01,  2.61995955e-01,  1.60792901e-01, -5.71680642e-01,\n",
       "        3.51660059e-01,  1.11498006e+00,  1.18326826e+00,  1.06094106e+00,\n",
       "        5.10712630e-01, -9.38783998e-01, -5.46496141e-01,  5.90029971e-01,\n",
       "        1.48218524e+00,  1.02118104e-01,  2.65438049e-01,  3.19307433e-03,\n",
       "       -2.59501150e+00, -1.55556933e+00,  1.10299596e+00,  5.54736504e-01,\n",
       "       -1.28901164e+00,  3.85241648e-01, -1.71729173e+00, -1.01835313e+00,\n",
       "        5.16353173e-02,  5.03298710e-01, -5.43186231e-01, -5.06678417e-01,\n",
       "        7.29652833e-01,  4.34273363e-01, -1.13367361e+00,  1.42395334e+00,\n",
       "        2.66351537e-01, -8.54264393e-01, -5.50596561e-01, -6.19109859e-01,\n",
       "        1.03893339e+00, -9.10610825e-01,  5.29952567e-01, -8.47143615e-03,\n",
       "       -1.12903825e+00,  5.69854191e-01, -8.63391622e-01, -1.35614427e+00,\n",
       "       -5.71515569e-02, -1.08621122e-01,  1.65238409e+00, -1.35092808e+00,\n",
       "       -5.46096737e-01,  9.91400184e-01,  2.20099739e+00,  4.27899791e-01,\n",
       "        2.90468283e-01,  6.11953096e-01, -5.12450984e-01, -7.24230691e-01,\n",
       "        1.69288190e+00, -2.99339120e-01,  1.57172719e+00,  4.61444067e-01,\n",
       "       -6.73829701e-01, -1.14103626e+00, -1.22891798e+00, -1.15928246e+00,\n",
       "       -3.20829018e-01,  1.08834758e+00, -9.06203145e-01, -4.64152062e-01,\n",
       "       -5.13378373e-01,  1.61783768e+00, -8.16650606e-01,  2.44719605e-01,\n",
       "       -1.31109423e+00,  3.88406495e-01,  1.59237371e+00,  8.70399037e-01,\n",
       "        3.35249325e-01,  6.48959907e-01, -1.83151791e-01,  5.00241270e-01,\n",
       "        1.36882639e+00,  8.95091842e-01,  6.47293372e-01, -5.67878708e-01,\n",
       "       -5.79517447e-01, -7.51448573e-01,  1.07551918e+00, -6.21142360e-01,\n",
       "        1.87855572e+00,  1.26023993e+00,  3.10050973e-01,  1.06402292e-01,\n",
       "        2.48012997e-01, -1.39383959e+00, -6.69436307e-01, -5.66791474e-01,\n",
       "       -3.81778903e-01, -9.46546907e-01, -1.06510300e+00, -1.33182618e+00,\n",
       "       -9.86453191e-01, -3.78391147e-01,  7.64711975e-01,  6.03594165e-02,\n",
       "        6.18509999e-01, -4.84921020e-01, -2.80530240e-01,  4.06962904e-01,\n",
       "        1.02518779e+00,  2.54751681e-01,  8.75239905e-02,  7.06983543e-02,\n",
       "       -5.73152603e-01,  1.22892597e+00, -9.62201893e-01,  1.52555676e+00,\n",
       "        8.27282589e-01,  9.12470470e-01, -1.27292343e-01,  6.34316641e-01,\n",
       "       -1.53089843e+00, -1.29070149e+00, -5.26228341e-01, -1.13223396e+00,\n",
       "       -4.99797127e-01, -7.28463087e-01, -5.83144170e-01,  3.29290657e-01,\n",
       "       -8.26860798e-01, -5.36867983e-01, -5.62980134e-01,  9.18404800e-01,\n",
       "       -7.93993782e-02, -2.78624683e-01, -1.30459539e-01, -1.39699761e+00,\n",
       "       -2.44713889e-01,  8.30253911e-01,  2.40821202e-01, -9.15697123e-01,\n",
       "       -2.22527996e+00, -6.63067012e-01, -3.21194764e-01,  4.98388165e-01,\n",
       "        3.80338976e-01, -1.06703532e+00,  2.55452172e-01,  2.11128719e+00,\n",
       "       -6.34189962e-01,  1.36875577e+00, -9.70649489e-01,  6.54245334e-01,\n",
       "       -1.17189522e+00, -3.15987198e-03, -7.45604825e-01,  1.59829089e+00,\n",
       "       -9.13399998e-01,  2.40291209e+00, -5.89360262e-01,  1.07657442e-01,\n",
       "       -1.39297516e-01, -1.15992573e+00,  6.18964782e-01,  1.37389047e+00])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "draws"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "bins = pd.qcut(draws,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(-0.684, -0.0101], (-0.0101, 0.63], (-0.684, -0.0101], (-0.684, -0.0101], (0.63, 3.928], ..., (-0.0101, 0.63], (-0.684, -0.0101], (-2.9499999999999997, -0.684], (-0.0101, 0.63], (0.63, 3.928]]\n",
       "Length: 1000\n",
       "Categories (4, interval[float64]): [(-2.9499999999999997, -0.684] < (-0.684, -0.0101] < (-0.0101, 0.63] < (0.63, 3.928]]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "bins = pd.qcut(draws,4,labels=list('ABCD'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[B, C, B, B, D, ..., C, B, A, C, D]\n",
       "Length: 1000\n",
       "Categories (4, object): [A < B < C < D]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 1, 1, 3, 3, 2, 2, 3, 3, 3, 0, 2, 2, 3, 3, 0, 1, 3, 1, 1, 2,\n",
       "       3, 0, 1, 2, 2, 2, 2, 3, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 1, 0, 0,\n",
       "       0, 2, 2, 0, 2, 3, 2, 2, 1, 3, 3, 0, 0, 2, 3, 1, 3, 2, 2, 3, 3, 0,\n",
       "       1, 0, 1, 0, 0, 3, 3, 3, 3, 1, 1, 0, 0, 2, 2, 0, 3, 2, 3, 3, 0, 3,\n",
       "       1, 3, 2, 3, 1, 3, 2, 3, 2, 0, 2, 2, 0, 1, 1, 0, 1, 1, 3, 3, 1, 3,\n",
       "       1, 2, 3, 0, 0, 1, 2, 1, 3, 0, 0, 0, 2, 3, 1, 1, 2, 1, 1, 1, 0, 0,\n",
       "       3, 3, 2, 2, 3, 1, 1, 2, 0, 3, 2, 1, 2, 1, 1, 1, 3, 3, 0, 3, 1, 0,\n",
       "       2, 0, 1, 3, 3, 1, 1, 1, 0, 3, 0, 3, 1, 3, 3, 3, 3, 1, 3, 0, 0, 1,\n",
       "       0, 0, 2, 1, 1, 1, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 3, 2, 3, 3, 0, 0,\n",
       "       3, 2, 3, 1, 2, 1, 0, 0, 0, 1, 0, 1, 3, 2, 1, 3, 1, 3, 0, 2, 3, 1,\n",
       "       1, 2, 0, 0, 2, 3, 3, 2, 1, 2, 0, 0, 0, 3, 1, 0, 0, 2, 2, 2, 0, 1,\n",
       "       3, 1, 3, 0, 2, 1, 3, 1, 3, 0, 1, 0, 2, 0, 1, 1, 0, 2, 0, 3, 1, 3,\n",
       "       0, 0, 1, 2, 1, 2, 2, 1, 0, 2, 2, 1, 0, 0, 1, 1, 1, 1, 2, 0, 3, 0,\n",
       "       0, 3, 0, 2, 1, 2, 0, 2, 2, 1, 3, 3, 1, 3, 3, 0, 1, 1, 2, 2, 3, 0,\n",
       "       0, 1, 3, 3, 1, 2, 2, 1, 2, 1, 3, 3, 1, 3, 3, 1, 0, 2, 3, 0, 1, 0,\n",
       "       2, 0, 0, 3, 0, 0, 3, 0, 2, 3, 3, 0, 0, 3, 2, 3, 3, 1, 1, 0, 1, 0,\n",
       "       3, 3, 3, 3, 1, 0, 0, 3, 2, 2, 3, 0, 1, 3, 0, 0, 3, 1, 0, 0, 3, 2,\n",
       "       2, 1, 1, 2, 0, 3, 2, 1, 0, 0, 3, 2, 3, 0, 0, 2, 2, 3, 1, 3, 1, 2,\n",
       "       0, 3, 1, 2, 3, 2, 2, 1, 3, 2, 2, 2, 1, 1, 3, 3, 0, 0, 0, 0, 0, 2,\n",
       "       0, 0, 1, 1, 1, 2, 2, 3, 3, 3, 0, 0, 2, 2, 1, 2, 1, 0, 2, 2, 3, 3,\n",
       "       1, 3, 2, 2, 0, 0, 2, 2, 0, 2, 0, 1, 2, 1, 1, 2, 0, 3, 0, 0, 3, 0,\n",
       "       2, 1, 0, 2, 1, 2, 2, 2, 2, 1, 0, 2, 1, 2, 0, 1, 0, 3, 3, 2, 1, 3,\n",
       "       2, 0, 0, 2, 2, 1, 1, 3, 1, 3, 3, 1, 3, 2, 3, 3, 0, 1, 0, 2, 2, 0,\n",
       "       1, 2, 3, 0, 1, 0, 3, 2, 0, 2, 2, 2, 2, 2, 0, 0, 3, 2, 3, 0, 3, 3,\n",
       "       1, 0, 3, 1, 1, 2, 2, 2, 3, 1, 0, 1, 0, 1, 1, 1, 2, 2, 2, 2, 2, 0,\n",
       "       3, 1, 0, 3, 0, 3, 0, 3, 0, 2, 2, 3, 3, 2, 2, 3, 0, 2, 1, 0, 2, 1,\n",
       "       1, 1, 2, 2, 0, 1, 2, 1, 1, 2, 2, 3, 1, 1, 1, 1, 0, 3, 3, 2, 1, 0,\n",
       "       2, 1, 2, 2, 1, 1, 3, 1, 1, 3, 0, 3, 2, 1, 0, 3, 0, 0, 1, 3, 0, 0,\n",
       "       3, 1, 1, 0, 2, 2, 0, 3, 2, 3, 3, 2, 0, 3, 2, 1, 1, 3, 1, 3, 3, 1,\n",
       "       2, 2, 0, 0, 3, 0, 0, 1, 0, 3, 0, 2, 3, 0, 1, 3, 0, 3, 3, 0, 1, 3,\n",
       "       1, 1, 0, 2, 3, 1, 2, 2, 2, 0, 3, 1, 1, 0, 2, 2, 3, 1, 0, 2, 1, 2,\n",
       "       1, 1, 0, 1, 3, 1, 3, 1, 2, 0, 3, 1, 2, 0, 0, 2, 3, 0, 2, 3, 1, 1,\n",
       "       3, 2, 2, 2, 1, 2, 3, 0, 1, 2, 2, 1, 3, 0, 0, 3, 0, 3, 0, 2, 1, 0,\n",
       "       3, 2, 3, 1, 0, 3, 2, 1, 2, 0, 3, 1, 1, 0, 2, 0, 1, 2, 3, 2, 0, 1,\n",
       "       0, 3, 1, 2, 2, 3, 3, 1, 3, 1, 1, 1, 1, 0, 2, 1, 2, 1, 2, 3, 0, 1,\n",
       "       0, 1, 3, 3, 3, 1, 2, 3, 0, 3, 3, 3, 0, 2, 3, 0, 2, 0, 1, 2, 2, 1,\n",
       "       2, 3, 1, 0, 2, 0, 1, 2, 0, 1, 3, 1, 3, 2, 0, 0, 3, 3, 2, 3, 2, 0,\n",
       "       2, 2, 3, 1, 3, 2, 1, 1, 2, 2, 3, 3, 0, 1, 2, 2, 1, 0, 0, 3, 3, 1,\n",
       "       3, 2, 2, 1, 2, 3, 3, 3, 2, 0, 1, 2, 3, 2, 2, 2, 0, 0, 3, 2, 0, 2,\n",
       "       0, 0, 2, 2, 1, 1, 3, 2, 0, 3, 2, 0, 1, 1, 3, 0, 2, 2, 0, 2, 0, 0,\n",
       "       1, 1, 3, 0, 1, 3, 3, 2, 2, 2, 1, 0, 3, 1, 3, 2, 1, 0, 0, 0, 1, 3,\n",
       "       0, 1, 1, 3, 0, 2, 0, 2, 3, 3, 2, 3, 1, 2, 3, 3, 3, 1, 1, 0, 3, 1,\n",
       "       3, 3, 2, 2, 2, 0, 1, 1, 1, 0, 0, 0, 0, 1, 3, 2, 2, 1, 1, 2, 3, 2,\n",
       "       2, 2, 1, 3, 0, 3, 3, 3, 1, 3, 0, 0, 1, 0, 1, 0, 1, 2, 0, 1, 1, 3,\n",
       "       1, 1, 1, 0, 1, 3, 2, 0, 0, 1, 1, 2, 2, 0, 2, 3, 1, 3, 0, 3, 0, 2,\n",
       "       0, 3, 0, 3, 1, 2, 1, 0, 2, 3], dtype=int8)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bins.codes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "bins = pd.Series(bins,name='quartile')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      B\n",
       "1      C\n",
       "2      B\n",
       "3      B\n",
       "4      D\n",
       "5      D\n",
       "6      C\n",
       "7      C\n",
       "8      D\n",
       "9      D\n",
       "10     D\n",
       "11     A\n",
       "12     C\n",
       "13     C\n",
       "14     D\n",
       "15     D\n",
       "16     A\n",
       "17     B\n",
       "18     D\n",
       "19     B\n",
       "20     B\n",
       "21     C\n",
       "22     D\n",
       "23     A\n",
       "24     B\n",
       "25     C\n",
       "26     C\n",
       "27     C\n",
       "28     C\n",
       "29     D\n",
       "      ..\n",
       "970    B\n",
       "971    A\n",
       "972    B\n",
       "973    D\n",
       "974    C\n",
       "975    A\n",
       "976    A\n",
       "977    B\n",
       "978    B\n",
       "979    C\n",
       "980    C\n",
       "981    A\n",
       "982    C\n",
       "983    D\n",
       "984    B\n",
       "985    D\n",
       "986    A\n",
       "987    D\n",
       "988    A\n",
       "989    C\n",
       "990    A\n",
       "991    D\n",
       "992    A\n",
       "993    D\n",
       "994    B\n",
       "995    C\n",
       "996    B\n",
       "997    A\n",
       "998    C\n",
       "999    D\n",
       "Name: quartile, Length: 1000, dtype: category\n",
       "Categories (4, object): [A < B < C < D]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = pd.Series(draws).groupby(bins).agg(['count','min',max]).reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>quartile</th>\n",
       "      <th>count</th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>250</td>\n",
       "      <td>-2.949343</td>\n",
       "      <td>-0.685484</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B</td>\n",
       "      <td>250</td>\n",
       "      <td>-0.683066</td>\n",
       "      <td>-0.010115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>C</td>\n",
       "      <td>250</td>\n",
       "      <td>-0.010032</td>\n",
       "      <td>0.628894</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>D</td>\n",
       "      <td>250</td>\n",
       "      <td>0.634238</td>\n",
       "      <td>3.927528</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  quartile  count       min       max\n",
       "0        A    250 -2.949343 -0.685484\n",
       "1        B    250 -0.683066 -0.010115\n",
       "2        C    250 -0.010032  0.628894\n",
       "3        D    250  0.634238  3.927528"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-2.04707659e-01,  4.78943338e-01, -5.19438715e-01, -5.55730304e-01,\n",
       "        1.96578057e+00,  1.39340583e+00,  9.29078767e-02,  2.81746153e-01,\n",
       "        7.69022568e-01,  1.24643474e+00,  1.00718936e+00, -1.29622111e+00,\n",
       "        2.74991633e-01,  2.28912879e-01,  1.35291684e+00,  8.86429341e-01,\n",
       "       -2.00163731e+00, -3.71842537e-01,  1.66902531e+00, -4.38569736e-01,\n",
       "       -5.39741446e-01,  4.76985010e-01,  3.24894392e+00, -1.02122752e+00,\n",
       "       -5.77087303e-01,  1.24121276e-01,  3.02613562e-01,  5.23772068e-01,\n",
       "        9.40277775e-04,  1.34380979e+00, -7.13543985e-01, -8.31153539e-01,\n",
       "       -2.37023165e+00, -1.86076079e+00, -8.60757398e-01,  5.60145293e-01,\n",
       "       -1.26593449e+00,  1.19827125e-01, -1.06351245e+00,  3.32882716e-01,\n",
       "       -2.35941881e+00, -1.99542955e-01, -1.54199553e+00, -9.70735912e-01,\n",
       "       -1.30703025e+00,  2.86349747e-01,  3.77984111e-01, -7.53886535e-01,\n",
       "        3.31285650e-01,  1.34974221e+00,  6.98766888e-02,  2.46674110e-01,\n",
       "       -1.18616011e-02,  1.00481159e+00,  1.32719461e+00, -9.19261558e-01,\n",
       "       -1.54910644e+00,  2.21845987e-02,  7.58363145e-01, -6.60524328e-01,\n",
       "        8.62580083e-01, -1.00319021e-02,  5.00093559e-02,  6.70215594e-01,\n",
       "        8.52965032e-01, -9.55868852e-01, -2.34933207e-02, -2.30423388e+00,\n",
       "       -6.52468841e-01, -1.21830198e+00, -1.33260971e+00,  1.07462269e+00,\n",
       "        7.23641505e-01,  6.90001853e-01,  1.00154344e+00, -5.03087391e-01,\n",
       "       -6.22274225e-01, -9.21168608e-01, -7.26213493e-01,  2.22895546e-01,\n",
       "        5.13161009e-02, -1.15771947e+00,  8.16706936e-01,  4.33609606e-01,\n",
       "        1.01073695e+00,  1.82487521e+00, -9.97518248e-01,  8.50591099e-01,\n",
       "       -1.31577601e-01,  9.12414152e-01,  1.88210680e-01,  2.16946144e+00,\n",
       "       -1.14928205e-01,  2.00369736e+00,  2.96101523e-02,  7.95253156e-01,\n",
       "        1.18109754e-01, -7.48531548e-01,  5.84969738e-01,  1.52676573e-01,\n",
       "       -1.56565729e+00, -5.62540188e-01, -3.26641392e-02, -9.29006202e-01,\n",
       "       -4.82572646e-01, -3.62638461e-02,  1.09539006e+00,  9.80928477e-01,\n",
       "       -5.89487686e-01,  1.58170009e+00, -5.28734826e-01,  4.57001871e-01,\n",
       "        9.29968759e-01, -1.56927061e+00, -1.02248698e+00, -4.02826924e-01,\n",
       "        2.20486863e-01, -1.93401108e-01,  6.69158336e-01, -1.64898482e+00,\n",
       "       -2.25279725e+00, -1.16683222e+00,  3.53607102e-01,  7.02110171e-01,\n",
       "       -2.74569205e-01, -1.39142188e-01,  1.07657222e-01, -6.06545125e-01,\n",
       "       -4.17064408e-01, -1.70070368e-02, -1.22414528e+00, -1.80083991e+00,\n",
       "        1.63473620e+00,  9.89008302e-01,  4.57940143e-01,  5.55154410e-01,\n",
       "        1.30671972e+00, -4.40553570e-01, -3.01350280e-01,  4.98791490e-01,\n",
       "       -8.23991040e-01,  1.32056584e+00,  5.07964786e-01, -6.53437675e-01,\n",
       "        1.86979514e-01, -3.91725249e-01, -2.72292975e-01, -1.71414356e-02,\n",
       "        6.80320749e-01,  6.35512357e-01, -7.57176502e-01,  7.18085834e-01,\n",
       "       -3.04273076e-01, -1.67779025e+00,  4.26986085e-01, -1.56373985e+00,\n",
       "       -3.67487521e-01,  1.04591253e+00,  1.21995436e+00, -2.47699116e-01,\n",
       "       -4.16232132e-01, -1.16747004e-01, -1.84478762e+00,  2.06870785e+00,\n",
       "       -7.76967474e-01,  1.44016687e+00, -1.10557360e-01,  1.22738699e+00,\n",
       "        1.92078426e+00,  7.46433038e-01,  2.22465959e+00, -6.79400410e-01,\n",
       "        7.27368782e-01, -8.68730734e-01, -1.21385091e+00, -4.70630931e-01,\n",
       "       -9.19241697e-01, -8.38826689e-01,  4.35155305e-01, -5.57804717e-01,\n",
       "       -5.67454871e-01, -3.72641553e-01, -9.26556901e-01,  1.75510839e+00,\n",
       "        1.20980999e+00,  1.27002473e+00, -9.74378127e-01, -6.34709255e-01,\n",
       "       -3.95700752e-01, -2.89435900e-01, -7.34297072e-01, -7.28504679e-01,\n",
       "        8.38775073e-01,  2.66893213e-01,  7.21194339e-01,  9.10982642e-01,\n",
       "       -1.02090261e+00, -1.41341604e+00,  1.29660784e+00,  2.52275209e-01,\n",
       "        1.12748110e+00, -5.68363447e-01,  3.09362168e-01, -5.77385473e-01,\n",
       "       -1.16863407e+00, -8.25019972e-01, -2.64440949e+00, -1.52985803e-01,\n",
       "       -7.51921003e-01, -1.32609252e-01,  1.45729970e+00,  6.09511845e-01,\n",
       "       -4.93779257e-01,  1.23997988e+00, -1.35722140e-01,  1.43004181e+00,\n",
       "       -8.46852451e-01,  6.03282130e-01,  1.26357226e+00, -2.55490556e-01,\n",
       "       -4.45688380e-01,  4.68366681e-01, -9.61603924e-01, -1.82450454e+00,\n",
       "        6.25428156e-01,  1.02287238e+00,  1.10742460e+00,  9.09370895e-02,\n",
       "       -3.50108657e-01,  2.17957016e-01, -8.94813130e-01, -1.74149395e+00,\n",
       "       -1.05225574e+00,  1.43660279e+00, -5.76207386e-01, -2.42029443e+00,\n",
       "       -1.06232963e+00,  2.37372262e-01,  9.57369064e-04,  6.52531808e-02,\n",
       "       -1.36752411e+00, -3.02800519e-02,  9.40489321e-01, -6.42436751e-01,\n",
       "        1.04017925e+00, -1.08292226e+00,  4.29213588e-01, -2.36223669e-01,\n",
       "        6.41817816e-01, -3.31660557e-01,  1.39407223e+00, -1.07674194e+00,\n",
       "       -1.92465982e-01, -8.71187651e-01,  4.20851997e-01, -1.21141107e+00,\n",
       "       -2.58866912e-01, -5.81646850e-01, -1.26042063e+00,  4.64574793e-01,\n",
       "       -1.07024091e+00,  8.04222698e-01, -1.56735508e-01,  2.01039001e+00,\n",
       "       -8.87104430e-01, -9.77936232e-01, -2.67217350e-01,  4.83337822e-01,\n",
       "       -4.00332733e-01,  4.49880415e-01,  3.99593953e-01, -1.51574804e-01,\n",
       "       -2.55793406e+00,  1.60806841e-01,  7.65250677e-02, -2.97204166e-01,\n",
       "       -1.29427402e+00, -8.85180013e-01, -1.87496526e-01, -4.93560000e-01,\n",
       "       -1.15412964e-01, -3.50744607e-01,  4.46973764e-02, -8.97756316e-01,\n",
       "        8.90873502e-01, -1.15118516e+00, -2.61230270e+00,  1.14125019e+00,\n",
       "       -8.67135525e-01,  3.83583258e-01, -4.37030164e-01,  3.47488810e-01,\n",
       "       -1.23017904e+00,  5.71078139e-01,  6.00612128e-02, -2.25523994e-01,\n",
       "        1.34972614e+00,  1.35029973e+00, -3.86653322e-01,  8.65989542e-01,\n",
       "        1.74723360e+00, -1.41024614e+00, -3.78241525e-01, -3.45820667e-01,\n",
       "        3.80062465e-01,  1.88994673e-01,  1.32329820e+00, -2.26458859e+00,\n",
       "       -9.14978611e-01, -4.78964164e-01,  1.04718450e+00,  9.23948418e-01,\n",
       "       -1.14150141e-01,  4.05802443e-01,  2.88451808e-01, -4.34788492e-01,\n",
       "        3.58755633e-01, -3.88244944e-01,  2.12874629e+00,  1.40960468e+00,\n",
       "       -1.05434278e-01,  7.00428367e-01,  2.09285188e+00, -1.36971796e-01,\n",
       "       -9.30489423e-01,  3.27497234e-01,  1.30301308e+00, -1.40940236e+00,\n",
       "       -1.44125945e-01, -7.16414024e-01,  1.03614186e-01, -1.49571856e+00,\n",
       "       -1.17489356e+00,  2.61399909e+00, -6.89307399e-01, -7.51652636e-01,\n",
       "        6.36280961e-01, -1.15764416e+00,  6.14679924e-01,  1.02139111e+00,\n",
       "        6.68272492e-01, -8.09535482e-01, -9.08124573e-01,  1.51228939e+00,\n",
       "        9.51174320e-02,  1.18466855e+00,  6.37032934e-01, -5.39274587e-01,\n",
       "       -5.51009929e-02, -1.13592579e+00, -1.70506254e-01, -1.15808727e+00,\n",
       "        1.10459914e+00,  6.34238100e-01,  1.25968335e+00,  9.64930776e-01,\n",
       "       -4.34445938e-01, -8.79602860e-01, -6.94838230e-01,  1.22637405e+00,\n",
       "        4.57278649e-01,  1.15698625e-01,  1.01404235e+00, -1.13500772e+00,\n",
       "       -2.63370582e-01,  1.30642518e+00, -1.61084101e+00, -1.02662069e+00,\n",
       "        1.24157279e+00, -1.56759532e-01, -2.44909553e+00, -1.03394802e+00,\n",
       "        1.59953363e+00,  4.74070770e-01,  1.51325927e-01, -5.42173166e-01,\n",
       "       -4.75496217e-01,  1.06402612e-01, -1.30822819e+00,  2.17318475e+00,\n",
       "        5.64561217e-01, -1.90480765e-01, -9.16934390e-01, -9.75813606e-01,\n",
       "        2.21230269e+00,  7.39306826e-02,  1.81859456e+00, -1.58153100e+00,\n",
       "       -7.74363409e-01,  5.52936493e-01,  1.06061390e-01,  3.92752804e+00,\n",
       "       -2.55125599e-01,  8.54137315e-01, -3.64806532e-01,  1.31101737e-01,\n",
       "       -6.97613896e-01,  1.33564946e+00, -1.51038858e-01,  4.42937851e-01,\n",
       "        9.41571221e-01,  5.33363953e-01,  3.56266193e-01, -1.01153190e-02,\n",
       "        1.41575317e+00,  5.66105533e-01,  4.56487352e-01,  1.94788105e-01,\n",
       "       -6.55053757e-01, -5.65230094e-01,  3.17687312e+00,  9.59532542e-01,\n",
       "       -9.75339835e-01, -1.11674210e+00, -1.10437634e+00, -8.98755175e-01,\n",
       "       -1.36663236e+00,  4.51401732e-01, -1.58722181e+00, -7.32788830e-01,\n",
       "       -5.14562627e-01, -6.11876053e-01, -3.15986670e-02,  2.34127888e-01,\n",
       "        2.71936281e-01,  1.23076935e+00,  1.28830616e+00,  8.51813508e-01,\n",
       "       -1.52918061e+00, -1.55171499e+00,  2.97292917e-01,  3.44791201e-01,\n",
       "       -3.98103360e-01,  4.29404057e-01, -2.85944649e-01, -2.22843700e+00,\n",
       "        6.65587850e-02,  4.89964761e-01,  1.86792585e+00,  2.07043780e+00,\n",
       "       -2.45383227e-01,  7.62302108e-01,  1.29015475e-01,  6.27075914e-01,\n",
       "       -1.06223474e+00, -1.49950345e+00,  5.45154193e-01,  4.00823267e-01,\n",
       "       -1.94623039e+00,  5.05031784e-01, -9.10489138e-01, -2.19696285e-01,\n",
       "        4.08055383e-01, -6.03145411e-01, -3.61132594e-01,  5.64024524e-01,\n",
       "       -1.05661656e+00,  1.39195005e+00, -1.76126767e+00, -9.11633128e-01,\n",
       "        6.58339264e-01, -1.57946584e+00,  4.57705976e-01, -1.83268092e-01,\n",
       "       -6.85483896e-01,  1.06431315e-01, -3.18236084e-01,  4.33712973e-01,\n",
       "        5.71825929e-01,  5.67106029e-01,  8.15769654e-02, -3.02334871e-01,\n",
       "       -7.26916388e-01,  1.80335113e-01, -5.20208734e-01,  3.98092081e-01,\n",
       "       -9.16935074e-01, -8.26501345e-02, -1.93969081e+00,  1.40799436e+00,\n",
       "        1.51240649e+00,  5.26493120e-01, -2.66930833e-01,  8.62284048e-01,\n",
       "        8.38030660e-02, -1.87233890e+00, -9.62791017e-01,  8.00668292e-02,\n",
       "        1.28726442e-01, -4.79120340e-01, -6.40280504e-01,  7.45973801e-01,\n",
       "       -6.22547063e-01,  9.36289315e-01,  7.50018377e-01, -5.67150282e-02,\n",
       "        2.30067451e+00,  5.69497467e-01,  1.48940968e+00,  1.26425028e+00,\n",
       "       -7.61837213e-01, -3.31616898e-01, -1.75131543e+00,  6.28894111e-01,\n",
       "        2.82501864e-01, -1.33813943e+00, -5.00606850e-01,  1.21645030e-01,\n",
       "        1.70832347e+00, -9.70999448e-01, -6.19332343e-01, -7.26708132e-01,\n",
       "        1.22165542e+00,  5.03699288e-01, -1.38787408e+00,  2.04851420e-01,\n",
       "        6.03705216e-01,  5.45680309e-01,  2.35477019e-01,  1.11834994e-01,\n",
       "       -1.25150375e+00, -2.94934350e+00,  6.34634161e-01,  1.24157016e-01,\n",
       "        1.29762249e+00, -1.68693341e+00,  1.08953905e+00,  2.06088174e+00,\n",
       "       -2.41235326e-01, -9.47872180e-01,  6.76294029e-01, -6.53356162e-01,\n",
       "       -6.52295298e-01,  5.28827604e-01,  3.57793249e-01,  1.88649360e-01,\n",
       "        8.69416879e-01, -5.06674481e-02, -7.16364575e-01, -1.03258721e-01,\n",
       "       -1.14103658e+00, -5.00776901e-01, -3.89301370e-01, -4.73850530e-01,\n",
       "        1.28664304e-01,  1.53694305e-01,  4.44790058e-01,  1.28531667e-01,\n",
       "        2.52529866e-01, -9.40638663e-01,  1.00214545e+00, -5.25414984e-01,\n",
       "       -8.87400936e-01,  1.83131360e+00, -9.23029332e-01,  7.00537687e-01,\n",
       "       -8.92151198e-01,  2.30074000e+00, -8.17765299e-01,  5.13759632e-01,\n",
       "        6.23586943e-01,  1.48920593e+00,  1.94047867e+00,  5.43237129e-01,\n",
       "        5.06190912e-01,  1.66201449e+00, -1.18920250e+00,  9.35974490e-02,\n",
       "       -5.39163905e-01, -1.43739560e+00,  1.87937386e-01, -4.50454457e-01,\n",
       "       -5.16878232e-01, -9.56356677e-02,  3.16423805e-01,  6.03334657e-01,\n",
       "       -1.49459146e+00, -1.10894079e-01,  2.41289404e-01, -5.82645109e-01,\n",
       "       -2.41112652e-01,  2.36360537e-01,  1.24720725e-01,  1.04632598e+00,\n",
       "       -2.73091856e-01, -5.34834020e-01, -3.06563305e-01, -1.62242665e-01,\n",
       "       -1.08323220e+00,  7.08401493e-01,  1.52074304e+00,  2.90343183e-01,\n",
       "       -6.83066330e-01, -9.50312866e-01,  4.00709936e-01, -1.26071684e-01,\n",
       "        3.98204888e-01,  1.41638473e-01, -2.64141422e-01, -4.52212074e-01,\n",
       "        7.58201973e-01, -5.15583498e-01, -5.91202322e-01,  8.96745784e-01,\n",
       "       -9.71437524e-01,  1.84080991e+00,  1.53881232e-01, -2.74083943e-01,\n",
       "       -1.78492569e+00,  9.81006686e-01, -8.73717140e-01, -1.01563442e+00,\n",
       "       -4.11243537e-01,  1.46562117e+00, -1.00621906e+00, -9.02147762e-01,\n",
       "        7.52769143e-01, -4.90508527e-01, -5.24672210e-01, -6.99195861e-01,\n",
       "        3.52360939e-01,  6.81025983e-02, -9.30341707e-01,  8.45399560e-01,\n",
       "        1.64723816e-02,  8.44962955e-01,  1.85083395e+00,  2.20742409e-02,\n",
       "       -1.36917902e+00,  8.87203523e-01,  1.43311821e-02, -7.41547051e-02,\n",
       "       -4.85647878e-02,  1.23502145e+00, -4.33294924e-01,  1.39103546e+00,\n",
       "        8.20210741e-01, -2.47423465e-01,  3.02270746e-01,  5.43980361e-01,\n",
       "       -9.42368504e-01, -1.26638281e+00,  9.37249545e-01, -7.20102245e-01,\n",
       "       -1.59395154e+00, -3.75497816e-01, -9.58703834e-01,  7.94336400e-01,\n",
       "       -1.60510784e+00,  5.43710253e-01,  9.25166364e-01, -1.46962860e+00,\n",
       "       -3.99592346e-01,  1.41734264e+00, -8.97608668e-01,  1.84480502e+00,\n",
       "        1.25316821e+00, -1.49093242e+00, -2.77339246e-02,  1.37523596e+00,\n",
       "       -2.52081701e-02, -6.67880179e-01, -2.86801753e+00,  2.10688543e-01,\n",
       "        1.28715531e+00, -5.74305988e-01,  4.95326647e-01,  3.96049590e-01,\n",
       "        5.88798190e-01, -1.28175713e+00,  2.02992261e+00, -5.01944516e-01,\n",
       "       -1.59284566e-01, -1.49621630e+00,  1.14477139e-02,  4.19445985e-01,\n",
       "        2.05121388e+00, -3.68765333e-01, -1.68925468e+00,  1.47681161e-01,\n",
       "       -1.80998392e-01,  1.58059054e-01, -3.96615422e-01, -4.00236630e-01,\n",
       "       -8.24895666e-01, -2.44440446e-01,  1.21945743e+00, -4.33630492e-01,\n",
       "        8.61183873e-01, -3.34503693e-01,  1.59559959e-01, -9.84164476e-01,\n",
       "        7.54084974e-01, -2.84391662e-01,  3.24797530e-01, -8.85424602e-01,\n",
       "       -1.28089348e+00,  1.96109935e-01,  9.54644156e-01, -8.00971332e-01,\n",
       "        1.58514730e-02,  1.08755329e+00, -6.31242820e-01, -2.26893249e-02,\n",
       "        6.85879242e-01,  5.19179208e-01,  1.82701892e-01,  2.04647381e-01,\n",
       "       -2.65986356e-01, -2.27288704e-04,  1.23945232e+00, -8.19715256e-01,\n",
       "       -2.60388907e-01,  5.19140257e-01,  1.43091645e-01, -1.16677747e-01,\n",
       "        1.49674411e+00, -1.48427438e+00, -1.67118276e+00,  9.17173409e-01,\n",
       "       -7.58014151e-01,  2.06479240e+00, -8.50778396e-01,  4.99450713e-01,\n",
       "       -7.92663655e-02, -1.40329264e+00,  1.57894791e+00,  3.69028988e-04,\n",
       "        9.00884914e-01, -4.54869220e-01, -8.64546645e-01,  1.12911990e+00,\n",
       "        5.78744129e-02, -4.33738666e-01,  9.26976374e-02, -1.39782015e+00,\n",
       "        1.45782265e+00, -1.76756916e-01, -2.54240300e-01, -1.26343750e+00,\n",
       "        4.52262741e-01, -8.40117409e-01, -5.02678071e-01,  5.13392587e-01,\n",
       "        1.64165300e+00,  5.80790036e-01, -1.70734027e+00, -1.78355431e-01,\n",
       "       -8.28459954e-01,  1.28631168e+00, -4.06452362e-01,  1.56632047e-01,\n",
       "        5.21066804e-02,  9.55813177e-01,  7.43191501e-01, -4.86323084e-01,\n",
       "        1.92046727e+00, -6.52749023e-01, -1.73303777e-01, -3.60410082e-01,\n",
       "       -3.80413977e-01, -1.29813981e+00,  5.27919008e-01, -9.31002763e-02,\n",
       "        4.01184681e-01, -1.02583380e-01,  3.08690977e-02,  2.61610051e+00,\n",
       "       -7.85577945e-01, -5.06998121e-01, -2.01820572e+00, -6.76853138e-01,\n",
       "        2.66674368e+00,  1.45145615e+00,  6.34628855e-01, -5.02826864e-01,\n",
       "        5.12931659e-01,  1.75677937e+00, -9.74310801e-01,  6.80397048e-01,\n",
       "        9.55798726e-01,  1.50153548e+00, -7.56265648e-01,  4.73504604e-01,\n",
       "        1.71374345e+00, -1.14769922e+00,  2.90322050e-03, -1.10057036e+00,\n",
       "       -2.97531782e-01,  5.02409078e-01, -9.87418981e-04, -6.74560278e-01,\n",
       "        2.97958279e-01,  1.46557314e+00, -3.03628594e-01, -9.94479885e-01,\n",
       "        1.89889991e-01, -1.68402957e+00, -4.58380742e-01,  5.43405908e-01,\n",
       "       -1.18726426e+00, -4.12641693e-01,  1.17712535e+00, -3.13704165e-01,\n",
       "        1.57903162e+00,  3.75388236e-01, -1.56813882e+00, -9.00886519e-01,\n",
       "        6.52345519e-01,  8.71600314e-01,  2.68216170e-01,  9.47681220e-01,\n",
       "        1.47267588e-01, -1.77245546e+00,  5.92419611e-01,  9.03254745e-02,\n",
       "        6.51121454e-01, -8.11946962e-02,  8.01897603e-01,  1.39845227e-01,\n",
       "       -5.01002762e-01, -1.28302559e-01,  4.14605966e-01,  6.04577786e-01,\n",
       "        2.13409475e+00,  9.41187837e-01, -9.31456796e-01, -1.24667539e-01,\n",
       "        2.00696291e-01,  1.80256286e-01, -3.20370097e-01, -1.59612803e+00,\n",
       "       -1.28169898e+00,  1.50258575e+00,  6.53538002e-01, -3.19536626e-01,\n",
       "        9.55094011e-01,  2.61995955e-01,  1.60792901e-01, -5.71680642e-01,\n",
       "        3.51660059e-01,  1.11498006e+00,  1.18326826e+00,  1.06094106e+00,\n",
       "        5.10712630e-01, -9.38783998e-01, -5.46496141e-01,  5.90029971e-01,\n",
       "        1.48218524e+00,  1.02118104e-01,  2.65438049e-01,  3.19307433e-03,\n",
       "       -2.59501150e+00, -1.55556933e+00,  1.10299596e+00,  5.54736504e-01,\n",
       "       -1.28901164e+00,  3.85241648e-01, -1.71729173e+00, -1.01835313e+00,\n",
       "        5.16353173e-02,  5.03298710e-01, -5.43186231e-01, -5.06678417e-01,\n",
       "        7.29652833e-01,  4.34273363e-01, -1.13367361e+00,  1.42395334e+00,\n",
       "        2.66351537e-01, -8.54264393e-01, -5.50596561e-01, -6.19109859e-01,\n",
       "        1.03893339e+00, -9.10610825e-01,  5.29952567e-01, -8.47143615e-03,\n",
       "       -1.12903825e+00,  5.69854191e-01, -8.63391622e-01, -1.35614427e+00,\n",
       "       -5.71515569e-02, -1.08621122e-01,  1.65238409e+00, -1.35092808e+00,\n",
       "       -5.46096737e-01,  9.91400184e-01,  2.20099739e+00,  4.27899791e-01,\n",
       "        2.90468283e-01,  6.11953096e-01, -5.12450984e-01, -7.24230691e-01,\n",
       "        1.69288190e+00, -2.99339120e-01,  1.57172719e+00,  4.61444067e-01,\n",
       "       -6.73829701e-01, -1.14103626e+00, -1.22891798e+00, -1.15928246e+00,\n",
       "       -3.20829018e-01,  1.08834758e+00, -9.06203145e-01, -4.64152062e-01,\n",
       "       -5.13378373e-01,  1.61783768e+00, -8.16650606e-01,  2.44719605e-01,\n",
       "       -1.31109423e+00,  3.88406495e-01,  1.59237371e+00,  8.70399037e-01,\n",
       "        3.35249325e-01,  6.48959907e-01, -1.83151791e-01,  5.00241270e-01,\n",
       "        1.36882639e+00,  8.95091842e-01,  6.47293372e-01, -5.67878708e-01,\n",
       "       -5.79517447e-01, -7.51448573e-01,  1.07551918e+00, -6.21142360e-01,\n",
       "        1.87855572e+00,  1.26023993e+00,  3.10050973e-01,  1.06402292e-01,\n",
       "        2.48012997e-01, -1.39383959e+00, -6.69436307e-01, -5.66791474e-01,\n",
       "       -3.81778903e-01, -9.46546907e-01, -1.06510300e+00, -1.33182618e+00,\n",
       "       -9.86453191e-01, -3.78391147e-01,  7.64711975e-01,  6.03594165e-02,\n",
       "        6.18509999e-01, -4.84921020e-01, -2.80530240e-01,  4.06962904e-01,\n",
       "        1.02518779e+00,  2.54751681e-01,  8.75239905e-02,  7.06983543e-02,\n",
       "       -5.73152603e-01,  1.22892597e+00, -9.62201893e-01,  1.52555676e+00,\n",
       "        8.27282589e-01,  9.12470470e-01, -1.27292343e-01,  6.34316641e-01,\n",
       "       -1.53089843e+00, -1.29070149e+00, -5.26228341e-01, -1.13223396e+00,\n",
       "       -4.99797127e-01, -7.28463087e-01, -5.83144170e-01,  3.29290657e-01,\n",
       "       -8.26860798e-01, -5.36867983e-01, -5.62980134e-01,  9.18404800e-01,\n",
       "       -7.93993782e-02, -2.78624683e-01, -1.30459539e-01, -1.39699761e+00,\n",
       "       -2.44713889e-01,  8.30253911e-01,  2.40821202e-01, -9.15697123e-01,\n",
       "       -2.22527996e+00, -6.63067012e-01, -3.21194764e-01,  4.98388165e-01,\n",
       "        3.80338976e-01, -1.06703532e+00,  2.55452172e-01,  2.11128719e+00,\n",
       "       -6.34189962e-01,  1.36875577e+00, -9.70649489e-01,  6.54245334e-01,\n",
       "       -1.17189522e+00, -3.15987198e-03, -7.45604825e-01,  1.59829089e+00,\n",
       "       -9.13399998e-01,  2.40291209e+00, -5.89360262e-01,  1.07657442e-01,\n",
       "       -1.39297516e-01, -1.15992573e+00,  6.18964782e-01,  1.37389047e+00])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "draws"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "In [53]: N = 10000000\n",
    "In [54]: draws = pd.Series(np.random.randn(N))\n",
    "In [55]: labels = pd.Series(['foo', 'bar', 'baz', 'qux'] * (N // 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "categories = labels.astype('category')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "80000080"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "labels.memory_usage()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10000272"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "categories.memory_usage()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    foo\n",
       "1    bar\n",
       "2    baz\n",
       "3    qux\n",
       "4    foo\n",
       "5    bar\n",
       "6    baz\n",
       "7    qux\n",
       "8    foo\n",
       "9    bar\n",
       "dtype: category\n",
       "Categories (4, object): [bar, baz, foo, qux]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "categories[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 693 ms\n"
     ]
    }
   ],
   "source": [
    "%time _=labels.astype('category')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    a\n",
       "1    b\n",
       "2    c\n",
       "3    d\n",
       "4    a\n",
       "5    b\n",
       "6    c\n",
       "7    d\n",
       "dtype: category\n",
       "Categories (4, object): [a, b, c, d]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "In [60]: s = pd.Series(['a', 'b', 'c', 'd'] * 2)\n",
    "In [61]: cat_s = s.astype('category')\n",
    "In [62]: cat_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    1\n",
       "2    2\n",
       "3    3\n",
       "4    0\n",
       "5    1\n",
       "6    2\n",
       "7    3\n",
       "dtype: int8"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s.cat.codes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c', 'd'], dtype='object')"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s.cat.categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "In [65]: actual_categories = ['a', 'b', 'c', 'd', 'e']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "cat_s2 = cat_s.cat.set_categories(actual_categories)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    a\n",
       "1    b\n",
       "2    c\n",
       "3    d\n",
       "4    a\n",
       "5    b\n",
       "6    c\n",
       "7    d\n",
       "dtype: category\n",
       "Categories (5, object): [a, b, c, d, e]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "d    2\n",
       "c    2\n",
       "b    2\n",
       "a    2\n",
       "e    0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s2.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "d    2\n",
       "c    2\n",
       "b    2\n",
       "a    2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "In [70]: cat_s3 = cat_s[cat_s.isin(['a', 'b'])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    a\n",
       "1    b\n",
       "4    a\n",
       "5    b\n",
       "dtype: category\n",
       "Categories (4, object): [a, b, c, d]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    a\n",
       "1    b\n",
       "4    a\n",
       "5    b\n",
       "dtype: category\n",
       "Categories (2, object): [a, b]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s3.cat.remove_unused_categories()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "In [73]: cat_s = pd.Series(['a', 'b', 'c', 'd'] * 2,\n",
    "dtype='category')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a  b  c  d\n",
       "0  1  0  0  0\n",
       "1  0  1  0  0\n",
       "2  0  0  1  0\n",
       "3  0  0  0  1\n",
       "4  1  0  0  0\n",
       "5  0  1  0  0\n",
       "6  0  0  1  0\n",
       "7  0  0  0  1"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.get_dummies(cat_s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "In [75]: df = pd.DataFrame({'key': ['a', 'b', 'c'] * 4,\n",
    "....: 'value': np.arange(12.)})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>c</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>a</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>c</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>a</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>b</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>c</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>a</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>b</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>c</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   key  value\n",
       "0    a    0.0\n",
       "1    b    1.0\n",
       "2    c    2.0\n",
       "3    a    3.0\n",
       "4    b    4.0\n",
       "5    c    5.0\n",
       "6    a    6.0\n",
       "7    b    7.0\n",
       "8    c    8.0\n",
       "9    a    9.0\n",
       "10   b   10.0\n",
       "11   c   11.0"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = df.groupby('key').value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "key\n",
       "a    4.5\n",
       "b    5.5\n",
       "c    6.5\n",
       "Name: value, dtype: float64"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     4.5\n",
       "1     5.5\n",
       "2     6.5\n",
       "3     4.5\n",
       "4     5.5\n",
       "5     6.5\n",
       "6     4.5\n",
       "7     5.5\n",
       "8     6.5\n",
       "9     4.5\n",
       "10    5.5\n",
       "11    6.5\n",
       "Name: value, dtype: float64"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.transform(lambda x:x.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     18.0\n",
       "1     22.0\n",
       "2     26.0\n",
       "3     18.0\n",
       "4     22.0\n",
       "5     26.0\n",
       "6     18.0\n",
       "7     22.0\n",
       "8     26.0\n",
       "9     18.0\n",
       "10    22.0\n",
       "11    26.0\n",
       "Name: value, dtype: float64"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.transform(\"sum\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      0.0\n",
       "1      2.0\n",
       "2      4.0\n",
       "3      6.0\n",
       "4      8.0\n",
       "5     10.0\n",
       "6     12.0\n",
       "7     14.0\n",
       "8     16.0\n",
       "9     18.0\n",
       "10    20.0\n",
       "11    22.0\n",
       "Name: value, dtype: float64"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.transform(lambda x:x*2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     4.0\n",
       "1     4.0\n",
       "2     4.0\n",
       "3     3.0\n",
       "4     3.0\n",
       "5     3.0\n",
       "6     2.0\n",
       "7     2.0\n",
       "8     2.0\n",
       "9     1.0\n",
       "10    1.0\n",
       "11    1.0\n",
       "Name: value, dtype: float64"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.transform(lambda x:x.rank(ascending=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "def normalize(x):\n",
    "    return (x-x.mean())/x.std()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    -1.161895\n",
       "1    -1.161895\n",
       "2    -1.161895\n",
       "3    -0.387298\n",
       "4    -0.387298\n",
       "5    -0.387298\n",
       "6     0.387298\n",
       "7     0.387298\n",
       "8     0.387298\n",
       "9     1.161895\n",
       "10    1.161895\n",
       "11    1.161895\n",
       "Name: value, dtype: float64"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.transform(normalize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    -1.161895\n",
       "1    -1.161895\n",
       "2    -1.161895\n",
       "3    -0.387298\n",
       "4    -0.387298\n",
       "5    -0.387298\n",
       "6     0.387298\n",
       "7     0.387298\n",
       "8     0.387298\n",
       "9     1.161895\n",
       "10    1.161895\n",
       "11    1.161895\n",
       "Name: value, dtype: float64"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.apply(normalize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "normal = (df.value - g.transform('mean'))/g.transform('std')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    -1.161895\n",
       "1    -1.161895\n",
       "2    -1.161895\n",
       "3    -0.387298\n",
       "4    -0.387298\n",
       "5    -0.387298\n",
       "6     0.387298\n",
       "7     0.387298\n",
       "8     0.387298\n",
       "9     1.161895\n",
       "10    1.161895\n",
       "11    1.161895\n",
       "Name: value, dtype: float64"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "normal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2017-05-20 00:00:00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017-05-20 00:01:00</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2017-05-20 00:02:00</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2017-05-20 00:03:00</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2017-05-20 00:04:00</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2017-05-20 00:05:00</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2017-05-20 00:06:00</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2017-05-20 00:07:00</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2017-05-20 00:08:00</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2017-05-20 00:09:00</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2017-05-20 00:10:00</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2017-05-20 00:11:00</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>2017-05-20 00:12:00</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>2017-05-20 00:13:00</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>2017-05-20 00:14:00</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  time  value\n",
       "0  2017-05-20 00:00:00      0\n",
       "1  2017-05-20 00:01:00      1\n",
       "2  2017-05-20 00:02:00      2\n",
       "3  2017-05-20 00:03:00      3\n",
       "4  2017-05-20 00:04:00      4\n",
       "5  2017-05-20 00:05:00      5\n",
       "6  2017-05-20 00:06:00      6\n",
       "7  2017-05-20 00:07:00      7\n",
       "8  2017-05-20 00:08:00      8\n",
       "9  2017-05-20 00:09:00      9\n",
       "10 2017-05-20 00:10:00     10\n",
       "11 2017-05-20 00:11:00     11\n",
       "12 2017-05-20 00:12:00     12\n",
       "13 2017-05-20 00:13:00     13\n",
       "14 2017-05-20 00:14:00     14"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "In [89]: N = 15\n",
    "In [90]: times = pd.date_range('2017-05-20 00:00', freq='1min',\n",
    "periods=N)\n",
    "In [91]: df = pd.DataFrame({'time': times,\n",
    "....: 'value': np.arange(N)})\n",
    "In [92]: df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-05-20 00:00:00</th>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-05-20 00:05:00</th>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-05-20 00:10:00</th>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     value\n",
       "time                      \n",
       "2017-05-20 00:00:00      5\n",
       "2017-05-20 00:05:00      5\n",
       "2017-05-20 00:10:00      5"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.set_index('time').resample('5t').count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>time</th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>a</td>\n",
       "      <td>2017-05-20 00:00:00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>2017-05-20 00:00:00</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>c</td>\n",
       "      <td>2017-05-20 00:00:00</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>a</td>\n",
       "      <td>2017-05-20 00:01:00</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b</td>\n",
       "      <td>2017-05-20 00:01:00</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>c</td>\n",
       "      <td>2017-05-20 00:01:00</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>a</td>\n",
       "      <td>2017-05-20 00:02:00</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key                time  value\n",
       "0   a 2017-05-20 00:00:00    0.0\n",
       "1   b 2017-05-20 00:00:00    1.0\n",
       "2   c 2017-05-20 00:00:00    2.0\n",
       "3   a 2017-05-20 00:01:00    3.0\n",
       "4   b 2017-05-20 00:01:00    4.0\n",
       "5   c 2017-05-20 00:01:00    5.0\n",
       "6   a 2017-05-20 00:02:00    6.0"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "In [94]: df2 = pd.DataFrame({'time': times.repeat(3),\n",
    "....: 'key': np.tile(['a', 'b', 'c'], N),\n",
    "....: 'value': np.arange(N * 3.)})\n",
    "In [95]: df2[:7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: FutureWarning: pd.TimeGrouper is deprecated and will be removed; Please use pd.Grouper(freq=...)\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    }
   ],
   "source": [
    "time_key = pd.TimeGrouper('5T')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = df2.set_index('time').groupby(['key',time_key]).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>value</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>key</th>\n",
       "      <th>time</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">a</th>\n",
       "      <th>2017-05-20 00:00:00</th>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-05-20 00:05:00</th>\n",
       "      <td>105.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-05-20 00:10:00</th>\n",
       "      <td>180.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">b</th>\n",
       "      <th>2017-05-20 00:00:00</th>\n",
       "      <td>35.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-05-20 00:05:00</th>\n",
       "      <td>110.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-05-20 00:10:00</th>\n",
       "      <td>185.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">c</th>\n",
       "      <th>2017-05-20 00:00:00</th>\n",
       "      <td>40.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-05-20 00:05:00</th>\n",
       "      <td>115.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-05-20 00:10:00</th>\n",
       "      <td>190.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         value\n",
       "key time                      \n",
       "a   2017-05-20 00:00:00   30.0\n",
       "    2017-05-20 00:05:00  105.0\n",
       "    2017-05-20 00:10:00  180.0\n",
       "b   2017-05-20 00:00:00   35.0\n",
       "    2017-05-20 00:05:00  110.0\n",
       "    2017-05-20 00:10:00  185.0\n",
       "c   2017-05-20 00:00:00   40.0\n",
       "    2017-05-20 00:05:00  115.0\n",
       "    2017-05-20 00:10:00  190.0"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
